package cn.sciento.transfer.api.controller.v1;

import cn.sciento.core.domain.Page;
import cn.sciento.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import cn.sciento.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import cn.sciento.boot.platform.lov.annotation.ProcessLovValue;
import cn.sciento.core.base.BaseController;
import cn.sciento.core.util.Results;
import cn.sciento.mybatis.domian.SecurityToken;
import cn.sciento.mybatis.helper.SecurityTokenHelper;
import cn.sciento.starter.keyencrypt.core.Encrypt;
import cn.sciento.transfer.app.service.ConsDbConfigService;
import cn.sciento.transfer.domain.entity.ConsDbConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = {"Consumer Db Config (Site Level)"})
@RestController("consDbConfigSiteController.v1")
@RequestMapping({"/v1/db-cons"})
public class ConsDbConfigSiteController extends BaseController {
  @Autowired
  private ConsDbConfigService consDbConfigService;
  
  @ApiOperation("新增数据消息消费DB配置")
  @Permission(level = ResourceLevel.SITE)
  @PostMapping({"/{producerConfigId}"})
  public ResponseEntity<ConsDbConfig> createConsDbConfig(@Encrypt @RequestBody ConsDbConfig consDbConfig, @Encrypt @PathVariable("producerConfigId") Long producerConfigId) {
    consDbConfig.setProducerConfigId(producerConfigId);
    this.consDbConfigService.insertConsDbConfig(consDbConfig);
    return Results.success(consDbConfig);
  }
  
  @ApiOperation("删除数据消息消费DB配置")
  @Permission(level = ResourceLevel.SITE)
  @DeleteMapping
  public ResponseEntity removeConsDbConfig(@Encrypt @RequestBody List<ConsDbConfig> consDbConfigs) {
    SecurityTokenHelper.validToken(consDbConfigs);
    this.consDbConfigService.batchDeleteConsDbConfig(consDbConfigs);
    return Results.success();
  }
  
  @ApiOperation("更新数据消息消费DB配置")
  @Permission(level = ResourceLevel.SITE)
  @PutMapping({"/{consDbConfigId}"})
  public ResponseEntity<ConsDbConfig> updateConsDbConfig(@Encrypt @RequestBody ConsDbConfig consDbConfig, @Encrypt @PathVariable("consDbConfigId") Long consDbConfigId) {
    consDbConfig.setConsDbConfigId(consDbConfigId);
    SecurityTokenHelper.validToken((SecurityToken)consDbConfig, false);
    this.consDbConfigService.updateConsDbConfig(consDbConfig);
    return Results.success(consDbConfig);
  }
  
  @ApiOperation("分页当前生产配置下的消费配置")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping({"/{producerConfigId}/paging"})
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<Page<ConsDbConfig>> listConsDbConfig(@Encrypt ConsDbConfig consDbConfig, @ApiIgnore @SortDefault(value = {"consDbConfigId"}, direction = Sort.Direction.DESC) PageRequest pageRequest, @Encrypt @PathVariable("producerConfigId") Long producerConfigId) {
    consDbConfig.setProducerConfigId(producerConfigId);
    Page<ConsDbConfig> list = this.consDbConfigService.pageAndSortConsDbConfig(pageRequest, consDbConfig);
    return Results.success(list);
  }
  
  @ApiOperation("分页查询全部消费配置信息")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping({"/paging"})
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<Page<ConsDbConfig>> listAllConsDbConfig(@Encrypt ConsDbConfig consDbConfig, @ApiIgnore @SortDefault(value = {"consDbConfigId"}, direction = Sort.Direction.DESC) PageRequest pageRequest) {
    Page<ConsDbConfig> list = this.consDbConfigService.pageDataCheckTargetTable(pageRequest, consDbConfig);
    return Results.success(list);
  }
  
  @ApiOperation("详情查询数据消息消费DB配置")
  @Permission(level = ResourceLevel.SITE)
  @GetMapping({"/{consDbConfigId}"})
  @ProcessLovValue(targetField = {"body"})
  public ResponseEntity<ConsDbConfig> detailConsDbConfig(@Encrypt @PathVariable("consDbConfigId") Long consDbConfigId) {
    ConsDbConfig consDbConfig = this.consDbConfigService.selectConsDbConfig(consDbConfigId);
    return Results.success(consDbConfig);
  }
}
